package com.hizt.mmb.sms.controller.api;

import com.hizt.mmb.sms.client.UserInfoClient;
import com.hizt.mmb.sms.service.SmsService;
import com.hzit.common.exception.Assert;
import com.hzit.common.result.R;
import com.hzit.common.result.ResponseEnum;
import com.hzit.common.utils.RegexValidateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 作者：李嘉浩
 * 时间：2024-08-29-16:39
 * 功能：
 */

@RequestMapping("/api/sms")
@RestController
@Api("短信管理")
@Slf4j
public class SysController {

    @Autowired
    private SmsService smsService;
    @Autowired
    private UserInfoClient userInfoClient;

    @GetMapping("/send/{phone}")
    @ApiOperation("验证码管理")
    public R send(@PathVariable @ApiParam(value = "电话号码",required = true) String phone) throws Exception {
        Assert.notNull(phone, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.isTrue(RegexValidateUtils.checkCellphone(phone),ResponseEnum.MOBILE_ERROR);
        R r = userInfoClient.checkMobile(phone);
        Assert.isTrue(r.getCode()==0,ResponseEnum.ERROR);
        boolean b = (boolean) r.getData().get("isExist");
        Assert.isTrue(!b,ResponseEnum.MOBILE_EXIST_ERROR);
        smsService.send(phone);
        return R.ok().message("验证码发送成功");
    }
}
